import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Brian
 * @author Harrison
 */
public class Analyst {

	protected List<Draft> myDraftList =  new ArrayList<>();

	public void addNewDraft(Draft aDraft) {

		myDraftList.add(aDraft);
	
	}
	/**
	 * @author Brian
	 * not used in the application
	 * 
	 */
	public int getAllDrafts(String name) {

		int i = 0;
		boolean found = false;

		while (i < myDraftList.size() && !found) {

			if (name.equalsIgnoreCase(myDraftList.get(i).getAnalystName())) {

				found = true;
			}

			else {
				i++;
			} 
		} 
		if (! found) {

			return -1;		
		}
		else {

			return i;
		} 
	}

	/**
	 * @Brian
	 * not used in the application
	 * */
	public String searchList(int index){

		String name = "";
		String draft = "";

		if (index>=0){

			name = myDraftList.get(index).getAnalystName();
			draft = myDraftList.get(index).getFileName();


		} else {

		}
		
		return name + " " + draft;
	}
	/**
	 * @author Brian
	 * ---Not used in the application--
	 * Method used to load drafts from file.
	 * 
	 * based on code from CS142
	 * South Puget Sound Community College
	 * 
	 * Instructor: Richard Hoagland
	 */
	@SuppressWarnings("unchecked")
	public void loadDrafts() {

		ObjectInputStream objectInput = null;
		FileInputStream fileInput = null;

		try {

			fileInput= new FileInputStream(new File("AnalystDraftStorageFile.txt"));
			objectInput = new ObjectInputStream(fileInput);

			Object obj = objectInput.readObject();
			myDraftList = (ArrayList<Draft>) obj;


		} catch (IOException e) {

			e.printStackTrace();

		} catch (ClassNotFoundException e) {

			e.printStackTrace();

		} finally {

			try {

				objectInput.close();
				fileInput.close();

			} catch (IOException e1) {

				e1.printStackTrace();
			} 
		}	
	}
	/**
	 * @author Brian
	 *  ---Not used in the application--
	 * Method used to save drafts
	 * 
	 * based on code from CS142
	 * South Puget Sound Community College
	 * 
	 * Instructor: Richard Hoagland
	 */
	public void saveDrafts() {

		FileOutputStream fileOutput = null;
		ObjectOutputStream objectOutput = null;

		try {

			fileOutput= new FileOutputStream(new File("AnalystDraftStorageFile.txt"));

			objectOutput = new ObjectOutputStream(fileOutput);
			objectOutput.writeObject(myDraftList); 
			objectOutput.writeObject(myDraftList); 

		} catch (IOException e) {

			e.printStackTrace();

		} finally {

			try {

				objectOutput.flush();
				objectOutput.close();

				fileOutput.close();

			} catch (IOException e1) {

				e1.printStackTrace();
			} 
		} 		
	}
	/**
	 * @author Brian
	 */
	public String toString() {

		StringBuilder sb = new StringBuilder();

		sb.append(myDraftList);

		return sb.toString();
	}

}
